﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TemplatesDatabase;

namespace Game
{
    public class GamePlayerDataInfo
    {
        public class StatAttribute : Attribute
        {
        }

        public Dictionary<string, Task> m_othertasks = new Dictionary<string, Task>();

        [Stat]
        public int Money;

        [Stat]
        public bool ShopVip;

        public IEnumerable<FieldInfo> GamePlayerDatas
        {
            get
            {
                foreach (FieldInfo item in from f in typeof(GamePlayerDataInfo).GetRuntimeFields()
                                           where f.GetCustomAttribute<StatAttribute>() != null
                                           select f)
                {
                    yield return item;
                }
            }
        }

        public void Load(ValuesDictionary valuesDictionary)
        {
            foreach (FieldInfo gamePlayerData in GamePlayerDatas)
            {
                if (valuesDictionary.ContainsKey(gamePlayerData.Name))
                {
                    object value = valuesDictionary.GetValue<object>(gamePlayerData.Name);
                    gamePlayerData.SetValue(this, value);
                }
            }

            ValuesDictionary value2 = valuesDictionary.GetValue("OtherTasks", new ValuesDictionary());
            foreach (KeyValuePair<string, object> item in value2)
            {
                Task task = ComponentTask.GetTask(item.Key, TaskType.Other);
                task.Conditions = (bool)item.Value;
                m_othertasks.Add(item.Key, task);
            }
        }

        public void Save(ValuesDictionary valuesDictionary)
        {
            foreach (FieldInfo gamePlayerData in GamePlayerDatas)
            {
                object value = gamePlayerData.GetValue(this);
                valuesDictionary.SetValue(gamePlayerData.Name, value);
            }

            ValuesDictionary valuesDictionary2 = new ValuesDictionary();
            valuesDictionary.SetValue("OtherTasks", valuesDictionary2);
            foreach (KeyValuePair<string, Task> othertask in m_othertasks)
            {
                valuesDictionary2.SetValue(othertask.Key, othertask.Value.Conditions);
            }
        }
    }
}